import { assert, expect } from 'chai'
import hre, { network } from 'hardhat'

const { id, networkName, ethers, ignition } = await network.connect()

describe('Counter', function () {
  it('Should emit the Increment event when calling the inc() function', async function () {
    const counter = await ethers.deployContract('Counter')
    console.log(`当前网络: ${id} ${networkName}, Counter合约地址: ${counter.target}`)
    await expect(await counter.inc())
      .to.emit(counter, 'Increment')
      .withArgs(1n)
  })

  it('The sum of the Increment events should match the current value', async function () {
    const counter = await ethers.deployContract('Counter')
    const deploymentBlockNumber = await ethers.provider.getBlockNumber()

    // run a series of increments
    for (let i = 1; i <= 10; i++) {
      await counter.incBy(i)
    }

    const events = await counter.queryFilter(counter.filters.Increment(), deploymentBlockNumber, 'latest')

    // check that the aggregated events match the current value
    let total = 0n
    for (const event of events) {
      total += event.args.by
    }

    expect(await counter.x()).to.equal(total)
  })
})
